using System;
using System.Windows.Forms;
using Sharpmark.PerfectLife.ColorfulPhotos.Framework.Picture;
using Sharpmark.PerfectLife.ColorfulPhotos.Framework.Utilities.ImageToText;
using System.Drawing;

namespace Sharpmark.PerfectLife.ColorfulPhotos.TextualPhoto
{
	public partial class FrmMain : Form
	{
		private const int DEFAULT_PHOTO_WIDTH = 200;
		private const int DEFAULT_PHOTO_HEIGHT = 200;

		private TextPhoto _photo;				// 读入的照片
		private bool _isKeepScale = true;		// 是否保持长宽比不变
		//private System.Drawing.Font _font;		//TOOD: 可选的字体

		public FrmMain()
		{
			InitializeComponent();
		}

		private decimal ScaleWidthByHeight(decimal h)
		{
			// 同比缩放长宽
			// *3 / 5 是因为大多数字体长宽比5:3，为了使图像显示比较正常
			return _photo.Photo.Width * h * 5 / _photo.Photo.Height / 3;
		}

		private decimal ScaleHeightByWidth(decimal w)
		{
			// 同比缩放长宽
			return _photo.Photo.Height * w * 3 / _photo.Photo.Width / 5;
		}

		#region 系统事件
		private void FrmMain_Load(object sender, EventArgs e)
		{
			// 关于
			this.Text = this.ProductName + " Ver" + this.ProductVersion;
			aboutIconPcb.Image	= this.Icon.ToBitmap();
			aboutTitleLbl.Text	= "About   : " + this.ProductName;
			aboutVersionLbl.Text= "Version : " + this.ProductVersion;
			aboutAuthorLbl.Text = "Author  : SharpMark\n";
			aboutEmailLbl.Text	= "Email   : Sharpmark@Gmail.com";

			//
			// 图像处理部分
			//
			_photo = new TextPhoto(new Photo(DEFAULT_PHOTO_WIDTH, DEFAULT_PHOTO_HEIGHT));

			this.photoIsKeepScaleCkb.Checked = _isKeepScale;
			this.photoGrayLevel8Rbn.Checked = true;

			//
			// 文字处理部分
			//
			foreach (FontFamily ff in FontFamily.Families)
			{
				stringFontListCbb.Items.Add(ff.Name);
			}
			stringFontListCbb.SelectedIndex = 0;
			//TODO: 处理异常
		}

		private void photoFileNameTxt_TextChanged(object sender, EventArgs e)
		{
			if (System.IO.File.Exists(this.photoFileNameTxt.Text))
			{
				_photo.Photo = new Photo(this.photoFileNameTxt.Text);

				// 当前文件可用，则将控件设为可用
				photoGroupCharsSize.Enabled = true;
				photoGroupGrayLevel.Enabled = true;
				photoGenerateBtn.Enabled = true;
				photoThumbnailViewBtn.Enabled = true;

				// 计算图像值
				photoCharsWidthNud.Value = _photo.Photo.Width / 3;
				if (!_isKeepScale)
					photoCharsHeightNud.Value = _photo.Photo.Height / 5;
			}
			else
			{
				// 当前文件不可用，则将控件设为不可用
				photoGroupCharsSize.Enabled = false;
				photoGroupGrayLevel.Enabled = false;
				photoGenerateBtn.Enabled = false;
				photoThumbnailViewBtn.Enabled = false;
			}
		}

		private void photoBrowerFileBtn_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == this.photoOpenImageFileOfd.ShowDialog())
			{
				this.photoFileNameTxt.Text = this.photoOpenImageFileOfd.FileName;
			}
		}

		private void photoThumbnailViewBtn_Click(object sender, EventArgs e)
		{
			(new FrmThumbnailView(photoFileNameTxt.Text)).Show();
		}

		private void photoCharsWidthNud_ValueChanged(object sender, EventArgs e)
		{
			_photo.CharsWidth = (int)photoCharsWidthNud.Value;
			if (_isKeepScale) photoCharsHeightNud.Value = ScaleHeightByWidth(photoCharsWidthNud.Value);
		}

		private void photoCharsHeightNud_ValueChanged(object sender, EventArgs e)
		{
			_photo.CharsHeight = (int)photoCharsHeightNud.Value;
			if (_isKeepScale) photoCharsWidthNud.Value = ScaleWidthByHeight(photoCharsHeightNud.Value);
		}

		private void photoGenerateBtn_Click(object sender, EventArgs e)
		{
			this.textGeneratedTxt.Text = _photo.GenerateText();
		}

		private void photoIsKeepScaleCkb_CheckedChanged(object sender, EventArgs e)
		{
			_isKeepScale = photoIsKeepScaleCkb.Checked;
			// 激活改变事件，使得长宽比自动调节
			photoCharsWidthNud_ValueChanged(photoCharsWidthNud, new EventArgs());
		}

		private void photoGrayLevelXXRbn_CheckChanged(object sender, EventArgs e)
		{
			_photo.GrayLevel = Int32.Parse(((RadioButton)sender).Tag.ToString());
		}

		private void photoAntiColorCkb_CheckedChanged(object sender, EventArgs e)
		{
			_photo.IsAntiColor = photoAntiColorCkb.Checked;
		}

		private void stringGenerateBtn_Click(object sender, EventArgs e)
		{
			Font font;
			try
			{
				FontStyle fs = FontStyle.Regular;
				if (stringFontBoldCkb.Checked) fs |= FontStyle.Bold;
				if (stringFontItalicCkb.Checked) fs |= FontStyle.Italic;
				if (stringFontUnderLineCkb.Checked) fs |= FontStyle.Underline;
				font = new Font(stringFontListCbb.SelectedItem.ToString(),
				24, fs, GraphicsUnit.Pixel);
			}
			catch (Exception)
			{ /* 有些字体Style不全 */
				font = new Font("Courier New", 24);
			}

			textGeneratedTxt.Text = (new TextString(
				(int)stringCharsWidthNud.Value, (int)stringCharsHeightNud.Value,
				stringBackgroundCharTxt.Text[0], stringFrontCharTxt.Text[0],
				stringContentTxt.Text, font)).GenerateText();
		}

		private void TextBox_SelectAll(object sender, EventArgs e)
		{
			((TextBox)sender).SelectAll();
		}

		private void NumbericUpDown_SelectAll(object sender, EventArgs e)
		{
			NumericUpDown nup = (NumericUpDown)sender;
			nup.Select(0, nup.Value.ToString().Length);
		}
		#endregion


	}
}